package com.starblink.android.basic.base.fragment

import android.os.Build
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.createViewModelLazy
import androidx.fragment.app.viewModels
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.RecyclerView
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.scwang.smart.refresh.layout.api.RefreshFooter
import com.scwang.smart.refresh.layout.simple.SimpleMultiListener
import com.starblink.android.basic.base.BaseViewModel
import com.starblink.android.basic.base.mvvm.ViewModelFactory
import com.starblink.android.basic.btmnavanim.BtmNavAnimControl
import java.lang.reflect.ParameterizedType

abstract class AbsVMFragment<VM : BaseViewModel> : AbsFragment() {

    protected lateinit var viewModel: VM

//    protected val viewModel by createViewModelLazy<VM>(
//        (((javaClass.genericSuperclass as ParameterizedType).actualTypeArguments[1]) as Class<VM>).kotlin,
//        {
//            this.viewModelStore
//        }) {
//        ViewModelFactory(this@AbsVMFragment)
//    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        createViewModel(savedInstanceState)
    }


    //反射获取ViewModel实例
    private fun createViewModel(savedInstanceState: Bundle?) {
        val type = javaClass.genericSuperclass
        if (type is ParameterizedType) {
            val tp = type.actualTypeArguments[1]
            val tClass = tp as? Class<VM> ?: BaseViewModel::class.java
            viewModel = ViewModelProvider(
                this.viewModelStore,
                ViewModelFactory(this, savedInstanceState)
            )[tClass] as VM
        }
        viewModel.viewBaseLD.observe(this) {
            if (it == BaseViewModel.POP_OWNER) {
                popSelf()
            }
        }
    }


    var btmNavAnimTag: String? = null

    /**
     * 观察者模式 注册一个目标给MainActivity的底部导航栏观察
     */
    protected fun showOrHideNavBarAnim(tag: String, scrollView: RecyclerView) {
        btmNavAnimTag = tag
        btmNavAnimTag?.let { tt ->
            BtmNavAnimControl.animControlAddSub(tt)
//            if (Build.VERSION.SDK_INT >= 23) {   //23版本才有此方法
//                scrollView.setOnScrollChangeListener { _, _, scrollY, _, oldScrollY ->
//                    BtmNavAnimControl.subMaps[tt]?.setNavAnimData(
//                        tt,
//                        scrollY - oldScrollY
//                    )
//                }
//            } else {
                scrollView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
                    override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                        super.onScrolled(recyclerView, dx, dy)
                        BtmNavAnimControl.subMaps[tt]?.setNavAnimData(tt, dy)
                    }

                    override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                        super.onScrollStateChanged(recyclerView, newState)
                        //停止滑动
                        if (newState == RecyclerView.SCROLL_STATE_IDLE){
                            BtmNavAnimControl.subMaps[tt]?.setNavAnimData(tt, 0)
                        }
                    }
                })
//            }
        }

    }

    override fun onDestroy() {
        btmNavAnimTag?.let {
            BtmNavAnimControl.removeSub(it)
        }
        if (refreshNavKey != null) {
            BtmNavAnimControl.removeSub(refreshNavKey!!)
        }
        super.onDestroy()
    }

    private var refreshNavKey: String? = null

    protected fun SmartRefreshLayout.bindHomeBar() {
        val refresherKey = this.hashCode().toString()
        BtmNavAnimControl.animControlAddSub(refresherKey)
        this.setOnMultiListener(object : SimpleMultiListener() {

            override fun onFooterMoving(
                footer: RefreshFooter?,
                isDragging: Boolean,
                percent: Float,
                offset: Int,
                footerHeight: Int,
                maxDragHeight: Int
            ) {
                super.onFooterMoving(
                    footer,
                    isDragging,
                    percent,
                    offset,
                    footerHeight,
                    maxDragHeight
                )
                BtmNavAnimControl.subMaps[refresherKey]?.setNavAnimData(refresherKey, offset)
            }

        })
        refreshNavKey = refresherKey
    }
}